package cn.spark.study.core

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
/*
 * scala: Accumulator累加变量 注意 sum 的复制是 +=
 */
object AccumulatorVariable {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("AccumulatorVariable").setMaster("local")
    
    val sc = new SparkContext(conf)
    val numbers = Array(1,2,3,4,5)
    val sum = sc.accumulator(0);
    
    sc.parallelize(numbers, 1).foreach { sum += _ }
    
    println(sum.value)
  }
}